package com.tuanzi.loan.web.bean.miniApps.articleLibrary;

import static com.tuanzi.loan.business.constants.DictionaryCode.API_SERIAL;
import static com.tuanzi.loan.business.constants.DictionaryCode.APPROVAL_TYPE;
import static com.tuanzi.loan.business.constants.DictionaryCode.CREDIT_CARD_TAG;
import static com.tuanzi.loan.business.constants.DictionaryCode.FIRST_PARTY_LOAN_TYPE;
import static com.tuanzi.loan.business.constants.DictionaryCode.IDENTITY_TAG;
import static com.tuanzi.loan.business.constants.DictionaryCode.LOAN_NECESSARY_DATA;
import static com.tuanzi.loan.business.constants.DictionaryCode.LOAN_PRODUCT_REPAY_TYPE;
import static com.tuanzi.loan.business.constants.DictionaryCode.PLATFORM;
import static com.tuanzi.loan.business.constants.DictionaryCode.PROMOTION_TYPE;
import static com.tuanzi.loan.business.constants.DictionaryCode.RATE_TYPE;
import static com.tuanzi.loan.business.constants.DictionaryCode.THIRD_PARTY_LONG_TAG;
import static com.tuanzi.loan.business.constants.DictionaryCode.UNION_INFO;
import static com.tuanzi.loan.business.constants.DictionaryCode.UNION_SERIAL;

import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletResponse;

import lombok.Data;
import lombok.EqualsAndHashCode;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.primefaces.event.FileUploadEvent;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.google.common.collect.Lists;
import com.tuanzi.loan.business.entity.miniApps.articleLibrary.MiniAppsRelation;
import com.tuanzi.loan.business.service.miniApps.articleLibrary.HealthHeadlineService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.vo.view.miniApps.articleLibrary.HealthHeadlineInfoView;
import com.tuanzi.loan.vo.view.miniApps.articleLibrary.MiniAppsArticleUrlVo;
import com.tuanzi.loan.web.configuration.JSFInfoBean;

/**
 * @author weibifeng 
 */
@Data
@Debug
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)
public class HealthHeadlineInfoBean extends JSFInfoBean<HealthHeadlineInfoView> {
	
    private static final long serialVersionUID = -2152778554108785342L;
    
    @Value("${mini_app_article_preview_url}")
	private String articlePreviewUrl;
    
    @Autowired
    private HealthHeadlineService healthHeadlineService;
    
    private List<MiniAppsRelation> allTabList = null; // 标签列表
    private List<MiniAppsRelation> allMiniAppsList = null; // 小程序列表

    
    @PostConstruct
    private void init() {
        String id = getParamFromRequest("id");
        if (StringUtils.isNotBlank(id)) {
            $ = healthHeadlineService.loadHealthHeadlineInfoView(NumberUtils.toLong(id));
        } else {
            $ = new HealthHeadlineInfoView();
            changeStatusToAdd();
        }
        
        allTabList = healthHeadlineService.getTabList();
        allMiniAppsList = healthHeadlineService.getMiniAppsList();
    }
    
    public void onPreview(Long id) {
    	FacesContext facesContext = getFacesContext();
		ExternalContext externalContext = facesContext.getExternalContext();
		
		try {
			HttpServletResponse response = (HttpServletResponse) externalContext.getResponse();
			response.sendRedirect(articlePreviewUrl + id);
		} catch (Exception e) {
			e.printStackTrace();
		} 
    }
    
    public void onCopyMedia(MiniAppsArticleUrlVo vo) {
    	$.setCopyMedia(new MiniAppsArticleUrlVo());
    	BeanUtils.copyProperties(vo, $.getCopyMedia());
    }
    
    public void onUploadContextConfImage(FileUploadEvent event) {
    	String imgUrl = this.uploadImg(event.getFile());
    	MiniAppsArticleUrlVo urlVo = healthHeadlineService.saveContentConfImg($, imgUrl);
    	$.getContentImgList().add(urlVo);
    	addSuccessfullyMessage();
    }
    
    public void onUploadContextConfVideo(FileUploadEvent event) {
    	String videoUrl = this.uploadImg(event.getFile());
    	MiniAppsArticleUrlVo urlVo = healthHeadlineService.saveContentConfVideo($, videoUrl);
    	$.getContentVideoList().add(urlVo);
    	addSuccessfullyMessage();
    }
    
    public void onUploadRecommendMainImage(FileUploadEvent event) {
    	$.getRecommendMainImage().setUploadUrl(this.uploadImg(event.getFile()));
    }

    public void onSave() {
    	healthHeadlineService.saveOrUpdHeadlineInfo($);
        changeStatusToView();
        addSuccessfullyMessage();
    }
    
    public void onSaveOrUpdRegion(){
//    	$.setSelectedRegionNodes(selectedRegionNodes);
//    	ProductInfoView view = productInfoService.saveOrUpdateRegion($);
//    	$.setProductInfoId(view.getProductInfoId());
    }


    public void onUploadBusinessLicense(FileUploadEvent event) {
//        $.setBusinessLicense(this.uploadImg(event.getFile()));
    }

    public void onUploadLogo(FileUploadEvent event) {
//        $.setLogo(this.uploadImg(event.getFile()));
    }

    public void onUploadMarketTag(FileUploadEvent event) {
//        $.setMarketTag(this.uploadImg(event.getFile()));
    }

    @Override
    protected List<String> getDictionaryCodes() {
        return Lists.newArrayList(FIRST_PARTY_LOAN_TYPE, APPROVAL_TYPE, CREDIT_CARD_TAG, IDENTITY_TAG, LOAN_NECESSARY_DATA, PROMOTION_TYPE, RATE_TYPE, UNION_INFO, UNION_SERIAL, API_SERIAL, PLATFORM ,THIRD_PARTY_LONG_TAG,LOAN_PRODUCT_REPAY_TYPE);
    }
    
    

}




